package cn.hallele.leinsteining.ms.support.starter.components.controller.io;

import cn.hallele.leinsteining.mc.infrastructure.metadata.constants.MsgCodeConst;
import cn.hallele.leinsteining.mc.infrastructure.metadata.io.Metric;
import cn.hallele.leinsteining.mc.infrastructure.metadata.io.Response;
import cn.hallele.leinsteining.mc.infrastructure.util.date.DateUtils;
import cn.hallele.leinsteining.ms.infrastructure.util.network.HttpUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.MessageSource;

import java.util.Objects;

/**
 * Res
 *
 * @author anle5
 * @since 2024/8/11 22:37
 */
public class Res<T> {
    private static final MessageSource I18N = SpringUtil.getBean(MessageSource.class);

    public static <T> Response<T> ok(T t) {
        return Response.<T>builder()
                .metric(getMetric(MsgCodeConst.SUCCESS))
                .data(t)
                .build();
    }

    private static Metric getMetric(String msgCode, Object... args) {
        return Metric.builder()
                .success(Objects.equals(MsgCodeConst.SUCCESS, msgCode))
                .ip(HttpUtil.getClientIp())
                .accessDevice(HttpUtil.getClientInfo())
                .msgCode(msgCode)
                .msgDesc(I18N.getMessage(msgCode, args, HttpUtil.getLocale()))
                .completedTime(DateUtils.now())
                .build();
    }
}
